$(function () {

	$(window.parent.document).find("#iframeReviewList").on('load',function(){
		var main = $(window.parent.document).find("#iframeReviewList");
		var mainHeight = $(this).contents().find("#reviewDiv").height()+10;
		main.height(mainHeight);
	});

	$("#btnSubscribeNow").click(function () {
		document.getElementById('light').style.display='block';
		document.getElementById('fade').style.display='block'
	});

	var vehicleTypeId = $('#smallCar').attr('data-id');
	var reservationDay = getNowFormatDay().replace("-","").replace("-","");
	var reservationStartTime = '';
	var reservationEndTime = '';

	var index = 0;
	var cost = $('#smallCar').attr('data-cost');
	if($.session.get('vehicleTypeId') != null){

		vehicleTypeId = $.session.get('vehicleTypeId');

		if ($.session.get('vehicleTypeId')== $('#smallCar').attr("data-id")){
			index = $('#carType').find("li[id = 'smallCar']").index();
			cost = $('#smallCar').attr('data-cost');

		}else if ($.session.get('vehicleTypeId')==$('#overSizeVehicle').attr("data-id")){
			index = $('#carType').find("li[id = 'overSizeVehicle']").index();
			cost = $('#overSizeVehicle').attr('data-cost');
		}
	}

	$('#cost').html(cost);
	setCarSelect(index);

	if ($.session.get('reservationDay') != null){
		reservationDay = $.session.get('reservationDay').replace("-","").replace("-","");
		$('#reservationDay').val($.session.get('reservationDay'));
	}

	if ($.session.get('reservationStartTime') != null){
		reservationStartTime = $.session.get('reservationStartTime').replace(":","");
		$('#reservationStartTime').val($.session.get('reservationStartTime'));
	}

	if ($.session.get('reservationEndTime') != null){
		reservationEndTime = $.session.get('reservationEndTime').replace(":","");
		$('#reservationEndTime').val($.session.get('reservationEndTime'));
	}

	// 获取可预约数
	getResidualNumber($('#stationId').val(), vehicleTypeId,reservationDay,reservationStartTime,reservationEndTime);


	// 汽车类型
	$("#carType li").click(function () {
		var index = $(this).index();

		var cost = $('#carType li').eq(index).attr('data-cost');

		var stationId = $('#stationId').val();
		var vehicleTypeId = $('#carType li').eq(index).attr('data-id');
		// 获取预约日期
		var reservationDay = $('#reservationDay').val().replace("-","").replace("-","");
		// 预约开始时间
		var reservationStartTime = $('#reservationStartTime').val().replace(":","");
		// 预约结束时间
		var reservationEndTime = $('#reservationEndTime').val().replace(":","");

		$('#cost').html(cost);

		// 获取可预约数
		getResidualNumber($('#stationId').val(), vehicleTypeId,reservationDay,reservationStartTime,reservationEndTime);

		setCarSelect(index);
	});
	// 地区
	$("#areaType li").click(function () {

		var index = $(this).index();
		// 点击的是全部时
		if(index == 0){
			// 除全部以外有未选中的区县时
			if(!$("#areaType li").eq(0).addClass("active").siblings().hasClass("active")){
				// 选中所有区县
				$("#areaType li").eq(0).addClass("active").siblings().addClass("active");
				// 所有区县全部选中时
			}else {
				// 除全部外，其余区县变为未选中
				$("#areaType li").eq(0).addClass("active").siblings().removeClass("active");
			}
			// 点击的是全部以外的区县
		}else {
			// 点击的区县是选中状态时
			if ($("#areaType li").eq(index).hasClass("active")){

				$("#areaType li").eq(index).removeClass("active");

				if (($("#areaType li").length) != $('#areaType').find("li[class = 'active']").length){
					$("#areaType li").eq(0).removeClass("active");
				}

				// 点击的区县是未选中状态
			}else{

				$("#areaType li").eq(index).addClass("active");

				if (($("#areaType li").length - 1) == $('#areaType').find("li[class = 'active']").length){
					$("#areaType li").eq(0).addClass("active");
				}else {
					$("#areaType li").eq(0).removeClass("active");
				}
			}
		}
	});
	// 单选
	$("#shopPayment").click( function () {
		$(this).siblings("span").addClass("active");
		$(this).parents("div").siblings("div").children("span").removeClass("active");
	});
	$("#onlinePayment").click( function () {
		$(this).siblings("span").addClass("active");
		$(this).parents("div").siblings("div").children("span").removeClass("active");
	});
	// 检测站tab
	$(".evaluate-list ul li").click(function(){
		var index = $(this).index();

		$(".evaluate-detail").eq(index).addClass("evaluate-detail-show").siblings().removeClass("evaluate-detail-show");
		$(".evaluate-list ul li").eq(index).addClass("active").siblings().removeClass("active");

		if (index != 0){
			$(".evaluate-list ul li").eq(index -1).children("span").addClass("active");
			$(".evaluate-list ul li").eq(index -1).siblings().children("span").removeClass("active");
		}
	})


})

function confirmPay(){
	alert("支付成功，是否进行车辆自检？");
}

// 根据预约日期对预约开始时间进行限定
function pickedFunc(){
	// 预约日期是今天时
	if ($('#reservationDay').val()==getNowFormatDay()){
		$('#reservationStartTime').val(getNowFormatStartTime());
		$('#reservationEndTime').val(getNowFormatEndTime());

		$('#reservationStartTime').attr("onclick","WdatePicker({dateFmt:'H:mm',minDate:'%H:%m'})");
	}else{

		$('#reservationStartTime').attr("onclick","WdatePicker({dateFmt:'H:mm'})");
	}
}

// 根据预约开始时间设定预约结束时间
function changeEndTime(){
	var startTime =  $('#reservationStartTime').val();
	if (startTime != null && startTime.length == 5){
		var startHour = startTime.substring(0,3);
		var endTime = startHour + "59";
		$('#reservationEndTime').val(endTime);
	}
}

// 获取当前日期并转化格式为YYYY-MM-DD
function getNowFormatDay() {
	// 当前日期
	var date = new Date();
	var seperator = "-";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	// 对月份进行补0
	if ( month >= 1 && month <= 9 ) {
		month = "0" + month;
	}
	// 对日进行补0
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentDate = year + seperator + month + seperator + strDate;
	return currentDate;
}

// 获取当前时间并转化格式为YYYY-MM-DD
function getNowFormatStartTime() {
	var date = new Date();
	var seperator = ":";
	var hour = date.getHours();
	var minutes = date.getMinutes();
	// 对小时进行补0
	if (hour < 10){
		hour = "0" + hour;
	}
	// 对分钟进行补0
	if (minutes < 10){
		minutes = "0" + minutes;
	}

	var currentTime = hour + seperator + minutes;
	return currentTime;
}

// 获取下个整点并转化格式为HH:MM
function getNowFormatEndTime() {
	var date = new Date();
	var seperator = ":";
	var hour = date.getHours();
	// 对小时进行补0
	if (hour < 10){
		hour = "0" + hour;
	}
	var endTime = hour + seperator + "59";
	return endTime;
}

function reservationDayChange(){
	searchResidualNumber()
}
function reservationStartTimeChange(){
	searchResidualNumber()
}
function reservationEndTimeChange(){
	searchResidualNumber()
}

function searchResidualNumber(){

	var stationId = $('#stationId').val();
	var vehicleTypeId = $('#smallCar').attr("data-id");
	if ($('#smallCar').attr("class") =="active"){
		vehicleTypeId = $('#smallCar').attr("data-id");
	}else if ($('#overSizeVehicle').attr("class") =="active"){
		vehicleTypeId = $('#overSizeVehicle').attr("data-id");
	}
	// 获取预约日期
	var reservationDay = $('#reservationDay').val().replace("-","").replace("-","");
	// 预约开始时间
	var reservationStartTime = $('#reservationStartTime').val().replace(":","");
	// 预约结束时间
	var reservationEndTime = $('#reservationEndTime').val().replace(":","");

	// 获取可预约数
	getResidualNumber($('#stationId').val(), vehicleTypeId,reservationDay,reservationStartTime,reservationEndTime);
}

//function changeFrameHeight(){
//	var ifm= document.getElementById("iframeReviewList");
//	//alert(ifm.contentWindow.document.body.scrollHeight);
//	ifm.style.height=ifm.contentWindow.document.documentElement.scrollHeight;
//
//}
//window.onresize=function(){
//	changeFrameHeight();
//}

function getResidualNumber(stationId,vehicleTypeId,reservationDay,reservationStartTime,reservationEndTime){

	$.ajax({
		type:'post',
		url:'/stationDetail/getResidualNumber',
		data:{"stationId":stationId,"vehicleTypeId":vehicleTypeId,"reservationDay":reservationDay,"reservationStartTime":reservationStartTime,"reservationEndTime":reservationEndTime},
		cache:false,
		dataType:'json',
		async:false,
		success:function(data){
			$('#residualNumber').html(data.data);
		}
	});

}

function setCarSelect(index){
	var $li = $("#carType li");
	$li.eq(index).addClass("active").siblings().removeClass("active");
	var _img = $li.eq(index).children( "img");
	var currentSrc = $(_img).attr("src");
	if(!(currentSrc.indexOf("_select") > -1)){
		_img.attr("src",currentSrc.split(".")[0]+"_select.png");
		var src_img = $li.eq(index).siblings().children("img");
		for(var i= 0; i< src_img.length;i++){
			var src =$(src_img[i]).attr("src");
			if(src.indexOf("_") > -1){
				$(src_img[i]).attr("src",src.split("_")[0]+".png");
			}
		}
	}
}




